package org.nanotek.tika.tests.writers;


import nl.flotsam.xeger.Xeger;

import org.apache.lucene.index.IndexWriter;
import org.nanotek.lucene.LuceneDocumentBuilder;
import org.nanotek.util.StringDistance;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.launch.JobExecutionNotRunningException;
import org.springframework.batch.core.launch.NoSuchJobException;
import org.springframework.batch.core.launch.NoSuchJobExecutionException;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class LuceneRandomWriterXerger {

	private static ClassPathXmlApplicationContext ctx;

	/**
	 * @param args
	 * @throws JobParametersInvalidException 
	 * @throws JobInstanceAlreadyCompleteException 
	 * @throws JobRestartException 
	 * @throws JobExecutionAlreadyRunningException 
	 * @throws NoSuchJobException 
	 * @throws JobExecutionNotRunningException 
	 * @throws NoSuchJobExecutionException 
	 */
	public static void main(String[] args)  {
		ctx = new ClassPathXmlApplicationContext(new String[] {
		"base-index-config/index_base_writer_context.xml"});
		int i = 0;
		try {
			String curResult = "";
			IndexWriter indexWriter = (IndexWriter) ctx.getBean("indexWriter");
			LuceneDocumentBuilder<String> documentBuilder = (LuceneDocumentBuilder<String>) ctx.getBean("documentBuilder");
			String regex = "[a-c]{6,6}";
			Xeger generator = new Xeger(regex);
			for (int j = 0 ; j < 1000;j++){
				System.out.println(" |||||");
				String result = "";
				for (i = 0 ; i < 100 ; i++){ 
//					String re1="((?:(?:[0-2]?\\d{1})|(?:[3][01]{1}))[-:\\/.](?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Sept|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?)[-:\\/.](?:(?:[1]{1}\\d{1}\\d{1}\\d{1})|(?:[2]{1}\\d{3})))(?![\\d])";	// DDMMMYYYY 1

					//				String result = "This is A PART:"  + generator.generate() + " " + new Date().getTime() ;
					curResult = generator.generate(); 
					result = result + " " + curResult;
//					if (i > 0){ 
//						int distance = StringDistance.getLevenshteinDistance(curResult, result);
//						curResult = result; 
//						System.out.println("LEV DISTANCE :" + distance);
//					}
//					System.err.println(currStr);
//					assert result.matches(regex);	
				}
				System.out.println("DOCUMENT IS ");
				System.out.println(result);
				//				indexWriter.addDocument(documentBuilder.buildDocument("This is A PART"  + i + " " + new Date().getTime()));
				indexWriter.addDocument(documentBuilder.buildDocument(result));
				System.out.println(" |||||");
			}
			indexWriter.commit();
			indexWriter.close();
		}catch (Throwable ex) 
		{ 
			System.out.println(i);
			ex.printStackTrace();
		}
	}

	Integer getRandomLen () 
	{  
		String valStr =  String.valueOf(new Double(Math.random()).floatValue());
		valStr = valStr.substring(0, 2);
		return new Integer(valStr);
	}

	//	int LevenshteinDistance(String s, String t)
	//	{
	//		int len_s = s.length(); 
	//		int len_t = t.length(); 
	//		int cost = 0;
	//
	//	  if(s[0] != t[0]) then cost = 1
	//
	//	  if(len_s == 0) then      return len_t
	//	  elseif(len_t == 0) then  return len_s
	//	  else                     return minimum(LevenshteinDistance(s[1..len_s-1], t) + 1,
	//	                                          LevenshteinDistance(s, t[1..len_t-1]) + 1,
	//	                                          LevenshteinDistance(s[1..len_s-1], t[1..len_t-1]) + cost)
	//	}

}
